﻿/*******************************************************
 *  文档授权管理 相关操作js
 * <p>Title: powerManager.js</p>
 * <p>Description:TODO</p>
 * @author Joychen
 * @date 2017年1月10日 下午5:41:57
 * @version 1.0
 * *****************************************************/
//角色管理
var $table,options, addOrEditUrl;
var powerManager = {
    //初始化页面
    initPage: function () {
        parent.showLoadBoxs();
        powerManager.initTable();
        powerManager.bindEvent();
        parent.delLoadBoxs();
     },
     //初始化Table
     initTable: function () {
         options = {
             url: ctx + "/DocEmpower/InitTable",
             cutHeight: 8,
             singleSelect:false,
             toolbar: "#exampleToolbar",
             queryParams: function (params) {
                 params = {
                     //页面大小  
                     rows: params.limit,
                     //第几页
                     page: params.offset / params.limit + 1,
                     powerName: encodeURI($('#powerName').val()),//关键字
                     startDate: $('#startDate').val(),//开始时间
                     endDate: $('#endDate').val(),//结束时间
                     powerStatus: $('#powerStatus').val()//状态
                 }
                 return params;
             },
             columns: [
                  {
                      radio: true
                  }, {
                      field: '',//第一列序号
                      title: '序号',
                      align: 'center',
                      width: 50,
                      formatter: function (value, row, index) {
                          return index + 1;
                      }
                  }, {
                      field: 'status',
                      title: '状态',
                      align: 'center',
                      formatter: function (value, row, index) {
                          switch (value) {
                              case 0: return "未生效"; break;
                              case 1: return "<span style='color:green;'>正常</span>"; break;
                              case 2: return "<span style='color:#FEB56F;'>10天到期</span>"; break;
                              case 3: return "<span style='color:red;'>封停</span>"; break;
                              case 4: return "<span style='color:red;'>逾期</span>"; break;
                              default: return "<span style='color:green;'>异常状态</span>"; break;
                          }
                      }
                  }, {
                      field: 'num_code',
                      title: '编号',
                      align: 'center'
                  }, {
                      field: 'user_name',
                      title: '姓名',
                      align: 'center'
                  }, {
                      field: 'login_name',
                      title: '登录名',
                      align: 'center'
                  }, {
                      field: 'user_phone',
                      title: '手机号',
                      align: 'center'
                  }, {
                      field: 'user_email',
                      title: '电子邮箱',
                      align: 'center'
                  }, {
                      field: 'start_date',
                      title: '开始日期',
                      align: 'center'
                  }, {
                      field: 'end_date',
                      title: '结束日期',
                      align: 'center'
                  }, {
                      field: 'reg_name',
                      title: '登记人',
                      align: 'center'
                  }, {
                      field: 'doc_note',
                      title: '备注',
                      align: 'center',
                      formatter: function (value, row, index) {
                          if (value && value.length > 20)
                              return "<span title='"+value+"'>" + value.substring(0, 20) + '...' + "</span>";
                          return value;
                      }
                  }],
             onClickRow: function (row, $element) {
             }
         }
         $table = tableHelper.initTable("exampleTableToolbar", options);
     },
     //绑定事件
     bindEvent: function () {
         //查询
         $('#btnSearch').click(function () {
             $table.bootstrapTable('refresh', options);
         });
         //详细
         $('#powerDetail').click(function () {
             var row = powerManager.selections(false);
             if (!row) 
                 return false;
             //清空表单
             $('#powerFormTable').resetHideValidForm();
             layer.open({
                 title: ['授权详细信息', 'font-size:18px;'],
                 type: 1,
                 content: $('#powerForm'),
                 scroll: true,//是否显示滚动条、默认不显示
                 btn: ['确定', '取消'],
                 area: ['530px', '90%'],
                 success: function (li, o) {
                     if (row) {
                         $('#powerFormTable')[0].reset();//重置表单
                         $('#powerFormTable').LoadForm(row);//表单填充数据
                     }
                 }
             });
         });
         //授权【添加】
         $('#powerAdd').click(function () {
             $('#autoBatch').css("display", "block");
             powerManager.editForm();
         });
         //编辑
         $('#powerEdit').click(function () {
             var row = powerManager.selections(false);
             if (!row)
                 return false;
             $('#autoBatch').css("display", "none");
             powerManager.editForm(row);
         });
         //删除
         $('#powerDel').on('click', function () {
             var row = powerManager.selections(true);
             if (!row) 
                 return false;
             var pId = [];
             $.each(row, function (index,item) {
                 pId.push(item.id);
             });
             layer.confirm('您确定要删除当前选择授权？', {
                 btn: ['确定', '取消'] //按钮
             }, function () {
                 var index = layer.msg("删除中...", { icon: 6, time: 6000 });
                 $.ajax({
                     type: 'post',
                     url: ctx + '/DocEmpower/DelPowerById',
                     data: { "pId": pId.join(",") },
                     dataType: 'json',
                     async: false,
                     success: function (data) {
                         layer.close(index);
                         if (data.Statu === 0) {
                             layer.msg("删除成功...");
                             $table.bootstrapTable('refresh', options);
                         }
                         else
                             layer.msg(data.Msg, { icon: 5 });
                     }
                 });
             }, function () { });
         });
         //批量导入
         $('#powerImport').on('click', function () {
             //清空表单
             $('#importFormTable').resetHideValidForm();
             //打开导入
             layer.open({
                 title: ['导入授权用户', 'font-size:18px;'],
                 type: 1,
                 content: $('#import_form'),
                 scroll: true,//是否显示滚动条、默认不显示
                 btn: ['确定', '取消'],
                 area: ['480px', '400px'],
                 success: function (li, o) {
                 },
                 yes: function (li, o) {
                     //判断文件
                     var fileSize = $("#excelFile")[0].files;
                     if (fileSize.length <= 0) {
                         layer.alert('请选择导入文件！', { icon: 5 });
                         return false;
                     }
                     if ($('#importFormTable').valid()) {
                         //验证通过
                         $('#importFormTable').ajaxSubmit({
                             url: ctx + "/DocEmpower/ImportUser",
                             type: "post",
                             dataType: "json",
                             beforeSubmit: function (arr, $form, options) {
                                 layer.msg("提交数据~", { icon: 1, time: 5000 });
                             },
                             success: function (result, status, xhr, $form) {
                                 if (result.Statu === 0) {
                                     layer.closeAll();
                                     $table.bootstrapTable('refresh', options);
                                     var str = new Array();
                                     if (result.Data) {
                                         $.each(result.Data, function (index, item) {
                                             str.push(item);
                                         });
                                     }
                                     layer.alert(result.Msg + (str.length > 0 ? "其中：" + str.join("、") : ""), { icon: 1 });
                                 }
                                 else {
                                     if (result.Data) {
                                         var str = new Array();
                                         $.each(result.Data, function (index, item) {
                                             str.push(item);
                                         });
                                         layer.alert(str.join("、"), { icon: 5 });
                                     } else
                                         layer.alert(result.Msg, { icon: 5 });
                                 }
                             }
                             //,
                             //error: function (xhr, status, error, $form) {
                             //    layer.msg("保存失败~", { icon: 5 });
                             //}
                         });
                     }
                 },
                 cancel: function (li, o) {

                 }
             });
         });
     },
     //初始化下拉
     initSelect: function () {
         $('#sm_code').initModuleSelectpicker();
         $('#smCode').initModuleSelectpicker();
         $('.selectpicker').selectpicker({ size: 5 });
     },
     //编辑、添加
     editForm: function (row) {
         var $title = "编辑授权信息";
         if (!row) {
             $title = "添加授权信息";
             addOrEditUrl = ctx + "/DocEmpower/AddPowerByObj";//新增
         }
         else
             addOrEditUrl = ctx + "/DocEmpower/EditPowerByObj";//编辑
         //清空表单
         $('#powerFormTable').resetHideValidForm();
         //打开编辑
         layer.open({
             title: [$title, 'font-size:18px;'],
             type: 1,
             content: $('#powerForm'),
             scroll: true,//是否显示滚动条、默认不显示
             btn: ['确定', '取消'],
             area: ['530px', '90%'],
             success: function (li, o) {
                 if (row != null) {
                     $('#powerFormTable')[0].reset();//重置表单
                     $('#powerFormTable').LoadForm(row);//表单填充数据
                 }
             },
             yes: function (li, o) {
                 //登录失去焦点事件：新增，编辑时改变代码都需要验证
                 powerManager.submitForm(addOrEditUrl);
             },
             cancel: function (li, o) {

             }
         });
     },
     submitForm: function (addOrEditUrl) {
         if ($('#powerFormTable').valid()) {
             //验证通过
             $('#powerFormTable').ajaxSubmit({
                 url: addOrEditUrl,
                 type: "post",
                 dataType: "json",
                 beforeSubmit: function (arr, $form, options) {
                     layer.msg("提交数据~", { icon: 1, time: 5000 });
                     return true;
                 },
                 success: function (result, status, xhr, $form) {
                     if (result.Statu === 0) {
                         layer.closeAll();
                         $table.bootstrapTable('refresh', options);
                         layer.msg('保存成功', { icon: 1 });
                     }
                     else
                         layer.alert(result.Msg, { icon: 5 });
                 }
                 //,
                 //error: function (xhr, status, error, $form) {
                 //    layer.msg("保存失败~", { icon: 5 });
                 //}
             });
         }
     },
    //3.0.0 获取选中行记录
     selections: function (isAll) {
         var row = $table.bootstrapTable('getSelections');
         if (!row || row.length === 0) {
             layer.msg("未选中行~", { icon: 5 });
             return false;
         }
         if (isAll)
             return row;
         if (row.length > 1) {
             layer.msg("当前操作不支持多选！", { icon: 5 });
             return false;
         }
         return row[0];
     }
 }